__version__ = "2.0.1"
__author__ = "ArtLinty"

# MIT License
#
# Copyright (c) 2019 立师兄Linty
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
# Usage：
# call build_driver() for web browser instance
# call build_request() for web api instance
"""

from base.box import _BoxDriver
from base.box import _BoxRequest
from base.helper import _CsvHelper, _PathHelper, _DbHelper, _YamlHelper, _JsonHelper
from base.infra import _Logger

"""
main portal, for web browser driver
"""


def build_driver(url, driver_type, wait_seconds=None):
    """
    get driver for web browser testing
    :param wait_seconds:
    :param url: str，访问浏览器的 url
    :param driver_type: str，指定浏览器的类型，忽略大小写
        : c, chrome
        : i, ie
        : f, firefox
        : o, opera
        : s, safari
        : h, headless, headless_chrome, hc
    :return: BoxDriver
    """
    if not isinstance(driver_type, str):
        driver_type = 'c'

    driver_type = _parse_type(driver_type.lower())
    if wait_seconds:
        driver = BoxDriver(driver_type, wait_seconds=wait_seconds)
    else:
        driver = BoxDriver(driver_type)
    driver.navigate(url)
    driver.maximize_window()
    return driver


"""
main portal, for web api request
"""


def build_request(url,
                  method,
                  headers=None,
                  cookies=None,
                  auth=None,
                  params=None):
    """
    get request for web api testing
    :param url: full api request url
    :param method: request method, str, 忽略大小写
    :param headers: request headers，dict
    :param cookies: request cookies， dict
    :param auth: request authorization， tuple，dict or str
    :param params: request params，dict， for all request methods
    :return: BoxRequest
    """

    # method type: str, or default p
    if not isinstance(method, str):
        method = 'p'
    return BoxRequest(url=url,
                      method=_parse_method(method).upper(),
                      headers=headers,
                      cookies=cookies,
                      auth=auth,
                      params=params)


"""
private method
"""


def build_logger(log_path):
    """
    build logger
    :param log_path:
    :return:
    """
    return Logger(
        log_path=log_path,
        call_path=__name__
    )


def _parse_type(driver_type):
    """
    parse driver type
    :param driver_type: str: driver type
        chrome: c or chrome
        firefox: f or firefox
        ie: i or ie
        safari: s or safari
        headless chrome: h or hc or headless, headless chrome
    :return: default value: BoxDriver.DriverType.CHROME
    """
    if driver_type == 'c' or driver_type == "chrome":
        return BoxDriver.DriverType.CHROME
    elif driver_type == 'f' or driver_type == "firefox":
        return BoxDriver.DriverType.FIREFOX
    elif driver_type == 'i' or driver_type == "ie":
        return BoxDriver.DriverType.IE
    elif driver_type == 's' or driver_type == "safari":
        return BoxDriver.DriverType.SAFARI
    elif driver_type == 'h' or driver_type == "headless" \
            or driver_type == "hc" or driver_type == "headless_chrome":
        return BoxDriver.DriverType.CHROME_HEADLESS

    return BoxDriver.DriverType.CHROME


def _parse_method(method):
    """
    parse request method
    :param method: str request method
        get: g or get
        post: p or post
        delete: d or delete
        put: u or put
        head: h or head
    :return: default value: POST
    """
    if method == 'g' or method == "get":
        return "GET"
    elif method == 'p' or method == "post":
        return "POST"
    elif method == 'd' or method == "delete":
        return "DELETE"
    elif method == 'u' or method == "put":
        return "PUT"
    elif method == 'h' or method == "head":
        return "HEAD"

    return "POST"


"""
public classes
"""


class BoxDriver(_BoxDriver):
    """
    BoxDriver
    """
    pass


class BoxRequest(_BoxRequest):
    """
    BoxRequest
    """
    pass


class Csv(_CsvHelper):
    """
    Csv
    """
    pass


class Db(_DbHelper):
    """
    Db
    """
    pass


class Path(_PathHelper):
    """
    Path
    """
    pass


class Yaml(_YamlHelper):
    """
    Yaml
    """
    pass


class Logger(_Logger):
    """
    Logger
    """
    pass


class Json(_JsonHelper):
    """
    Json 帮助类
    """
    pass
